# import jwt
# class Mytoken():
#     def __init__(self):
#         # 初始化密钥  也就是盐
#         self.password='aksjkdfghdoislkdfjuioshdfjwoeflksdbskfjlasfknsjdk'
#     def encode_jwt(self,userinfo):
#         # 生成jwt           用户信息  密钥          哈希算法
#         myjwt = jwt.encode(userinfo, self.password,algorithm='HS256')
#         try:
#             myjwt = str(myjwt,'utf8')
#         except:
#             pass
#         return myjwt
    
#     def decode(self,myjwt):
#         # 解密          用户信息  密钥          哈希256算法
#         return jwt.decode(myjwt,self.password,algorithms='HS256')
#         # 重新生成token
#         # 返回
        
#     # 对比
#     def check(self):
#         if token ==newtoken:
#             return True
#         else:
#             return False

# mytoken = Mytoken()
# # 生成
# token = mytoken.encode_jwt({'id':1,'username':'jch'})
# print(token)
# # 解密
# print(mytoken.decode(token))

# # import base64
# # import HAX


import jwt

class Myjwt():
    def __init__(self):
        self.password='aksjkdfghdoislkdfioshdfjwoeflksdbskfjlasfknsjdk'
    def encode_jwt(self,data):
        token = jwt.encode(data, self.password,algorithm='HS256')
        try:
            token = str(token,encoding='utf8')
        except:
            pass
        return token
    
    def decode_jwt(self,token):
        try:
            # 解密token
            data = jwt.decode(token,self.password,algorithms='HS256')
            # 重新生成token
            newtoken = jwt.encode(data,self.password,algorithm='HS256')
            return str(newtoken)
        except:
            return None
        
    # 进行token对比
    def check(self,token,newtoken):
        if token == newtoken:
            return True
        return False
myjwt = Myjwt()

# token = myjwt.jwt_encode({'data':{'id':1,'username':'qwe'}})
# print(token)
token = myjwt.encode_jwt({'data':{'name':'sdf'}})
print(token)
newtoken = myjwt.decode_jwt(token)

print(newtoken)
flag = myjwt.check(token,newtoken)
print(flag)
